package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.StoreBrandDAO;
import com.ecoolex.hotel.saas.entity.StoreBrand;
import com.ecoolex.hotel.saas.entity.StoreBrand.Column;
import com.ecoolex.hotel.saas.entity.StoreBrandExample;
import com.ecoolex.hotel.saas.entity.StoreBrandExample.Criteria;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class StoreBrandManager {
	
    @Autowired
    private StoreBrandDAO dao;
    
    @Autowired
    private StoreManager storeManager;
    
    @Cacheable("hotel-saas:StoreBrand")
    public Pagination<StoreBrand> query(String name, Page page) {
    	StoreBrandExample example = new StoreBrandExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNullOrEmpty(name)) {
    		criteria.andNameLike("%"+name+"%");
    	}
    	criteria.andDeletedEqualTo(StoreBrand.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

    @Cacheable("hotel-saas:StoreBrand")
	public StoreBrand get(Integer brandId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(brandId, StoreBrand.NOT_DELETED);
	}

    @CacheEvict(cacheNames = "hotel-saas:StoreBrand", allEntries = true)
	public StoreBrand add(StoreBrand record, String createUser){
    	record.setDeleted(StoreBrand.NOT_DELETED);
    	record.setCreateUser(createUser);
    	record.setCreateTime(Date.from(Instant.now()));
    	record.setUpdateUser(createUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.insert(record);
		return record;
	}

    @CacheEvict(cacheNames = "hotel-saas:StoreBrand", allEntries = true)
	public StoreBrand edit(StoreBrand record, String updateUser){
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

    @CacheEvict(cacheNames = "hotel-saas:StoreBrand", allEntries = true)
	public Boolean del(StoreBrand record, String updateUser){
		//检查是否存在关联的商户
		BizAssert.isTrue(storeManager.countByBrandId(record.getBrandId()) < 1,
			ResultCode.BIZ.build(1),
			"该品牌存在关联的商户数据，无法删除");
		//TODO 是否关联了优惠券？
		record.setDeleted(StoreBrand.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
		return true;
	}
}